// JavaScript Document
define(function (require, exports, module) {
	var Utils=function(){};

	Utils.getOrgCodeMask=function(orgCode){
		var ws = [3, 7, 9, 10, 5, 8, 4, 2]; 
		var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';  
		var sum = 0; 
		for (var i = 0; i < 8; i++) { 
			sum += str.indexOf(orgCode.charAt(i)) * ws[i]; 
		} 
		var C9 = 11 - (sum % 11); 
		if (C9 == 11) { 
			return '0'; 
		} else if (C9 == 10) {
			return 'X' ;
		} else { 
			return C9 ;
		}
	}
	
	
	Utils.loginCheck=function(key,delay){
		window.setInterval(function(){
			var value=this.getCookie(key);
			if(value=="true"){
				return true;
			}else{
				alert("未登录,请重新登录");
				top.location.href="/";
				return false;
			}
		},5000)
	}
	
	
	
	
	Utils.encodeXss=function(str){
		str = str.replace(/&/g, "&amp;");//&amp; 符号
		str = str.replace(/</g, "&lt;").replace(/>/g, "&gt;");//<>（尖括号）
        str = str.replace(/\(/g, "&#40;").replace(/\)/g, "&#41;");//()（括号）
        str = str.replace(/'/g, "&#39;");//\\'（反斜杠转义单引号）
        str = str.replace(/\|/g , "&brvbar;" );//| &#166; &brvbar;
        //str = str.replace(/\;/g ,"&#59;");//; &#59;
        str = str.replace(/\$/g ,"&#36;");//$ &#36;
        str = str.replace(/\%/g ,"&#37;");//% &#37;
        str = str.replace(/\@/g ,"&#64;");//@（at 符号） @ &#64; 
        str = str.replace(/\+/g ,"&#43;");//+ &#43
        str = str.replace(/,/g ,"&#44;");
        str = str.replace(/\\/g ,"&#92;");//\ &#92;
        str = str.replace(/\r/g ,"&#13;");//\r 回车
        str = str.replace(/\n/g ,"&#10;");//\n 换行
        str = str.replace(/\"/g ,"&#34;");
		return str;
	}
	Utils.decodeXss=function(str){
		str = str.replace(/&amp;/g, "&");//&amp; 符号
		str = str.replace(/&lt;/g, "<").replace(/&gt;/g, ">");//<>（尖括号）
        str = str.replace(/&#40;/g, "(").replace(/&#41;/g, ")");//()（括号）
        str = str.replace(/&#39;/g, "'");//\\'（反斜杠转义单引号）
        str = str.replace(/&brvbar;/g , "|" );//| &#166; &brvbar;
        str = str.replace(/&#59;/g ,";");//; &#59;
        str = str.replace(/&#36;/g ,"$");//$ &#36;
        str = str.replace(/&#37;/g ,"%");//% &#37;
        str = str.replace(/&#64;/g ,"@");//@（at 符号） @ &#64; 
        str = str.replace(/&#43;/g ,"+");//+ &#43
        str = str.replace(/&#44;/g ,",");
        str = str.replace(/&#92;/g ,"\\");//\ &#92;
        str = str.replace(/&#13;/g ,"\r");//\r 回车
        str = str.replace(/&#10;/g ,"\n");//\n 换行
        str = str.replace(/&#34;/g ,"\"");
		return str;
	}
	
	
	
	
	
	
	
	//添加父级的body className到body className
	Utils.appendParentBodyClassName=function(){
		var bodyClassName=document.body.className
		var parentBodyClassName=parent.document.body.className;
		if(bodyClassName!=parentBodyClassName){
			document.body.className=document.body.className+" "+parentBodyClassName;
		}
	}
	
	
	//添加window的name body className
	Utils.appendWindowNameToBodyClassName=function(){
		var windowName=window.name;
		if(windowName!=""){
			document.body.className=document.body.className+" "+windowName;
		}
	}
	
	
	
	
	
	
	
	Utils.moduleAutoNum=function(){
		var elements=document.getElementsByTagName("div")
		var className="";
		var spanElement;
		var j=0;
		for(var i=0;i<elements.length;i++){
			className=elements[i].className
			if(className.indexOf("module")>"-1"){
				if($(elements[i]).css('display')!="none"){
					spanElement=document.createElement("span")
					spanElement.className="No"
					spanElement.innerHTML=(j+1)
					j++;
					elements[i].appendChild(spanElement);
				}
			}
		}
	}
	
	
	
	
	
	
	
	
	Utils.getParentElementByTagName=function(context,tagName){
		var parentNode=context.parentNode;
		var parentNodeTagName=parentNode.tagName
		if(typeof parentNodeTagName=="undefined"){
			return context;
		}
		//alert(nodeName);
		if(parentNodeTagName!=tagName){
			return Utils.getParentElementByTagName(parentNode,tagName);
		}else{
			return parentNode;
		}
	}
	
	
	
	Utils.getRealLength=function(str){
		var length = $.trim(str).replace(/[^\x00-\xff]/g, "**").length;
	    return length; 
	}
	
	
	
	Utils.getRequiredStr=function(inputElement,rule){
		var type="";
		var requiredStr="";
		
		type=rule.type
		if(typeof type=="undefined"){
			type="text"
		}
		if(type=="text"){
			requiredStr="必填";
		}
		if(type=="select" || type=="selector" || type=="checkbox" || type=="radio"){
			requiredStr="必选";
		}
		return requiredStr;
	}
	
	Utils.cloneFormData=function(source,target){
		var serializeArray=$(source).serializeArray();
		var html="";
		var s;
		var name;
		var value;
		for(var i=0;i<serializeArray.length;i++){
			s=serializeArray[i];
			name=s.name;
			value=s.value;
			html+='<input type="hidden" name="'+name+'" value="'+value+'" />';
		}
		$(target).append(html);
	}
	module.exports = Utils;
});